package com.zyh.system.userinfo.service.impl;

import com.zyh.system.userinfo.dao.impl.UserInfoDaoImpl;
import com.zyh.system.userinfo.domain.User;
import com.zyh.system.userinfo.service.UserInfoService;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @Author zyh
 * @Date 2024/7/31 19:16
 * @Description UserInfoService实现类
 */

public class UserInfoServiceImpl implements UserInfoService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final UserInfoDaoImpl daoImpl = new UserInfoDaoImpl();

    /**
     * 添加信息功能
     *
     * @param list
     */
    @Override
    public void addUserInfo(LinkedList<User> list) {
        System.out.println("开始录入公民信息：");
        System.out.print("请输入姓名：");
        String userName = INPUT.next();
        String userId = null;
        while (true) {
            System.out.print("请输入身份证号：");
            userId = INPUT.next();
            if (userId.length() != 18) {
                System.out.println("身份证号输入有误！");
                continue;
            } else {
                break;
            }
        }
        String sex = null;
        while (true) {
            System.out.println("请输入性别(1：男，0：女)：");
            int sexNum = INPUT.nextInt();
            if (sexNum == 1) {
                sex = "男";
                break;
            } else if (sexNum == 0) {
                sex = "女";
                break;
            } else {
                System.out.println("输入有误，请重新输入！");
                continue;
            }
        }
        System.out.print("请输入地址：");
        String address = INPUT.next();
        User user = new User(userName, userId, sex, address);
        boolean b = daoImpl.addUserInfo(list, user);
        if (b) {
            System.out.println("录入成功！");
        } else {
            System.out.println("录入失败！");
        }
    }

    /**
     * 展示所有信息
     *
     * @param list
     */
    @Override
    public void showUserInfo(LinkedList<User> list) {
        if (list.isEmpty()) {
            System.out.println("请先添加公民信息后在查询！");
        } else {
            daoImpl.showUserInfo(list);
        }
    }

    /**
     * 修改信息
     * @param list
     */
    @Override
    public void updateUserInfo(LinkedList<User> list) {
        System.out.println("信息修改：");
        while (true) {
            System.out.print("请输入需要修改的人的身份证号：");
            String id = INPUT.next();
            //假设不存在
            boolean flag=false;
            int index=-1;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId().equals(id)){
                    flag=true;
                    index=i;
                    break;
                }
            }
            if (flag){
                System.out.print("请输入修改后的姓名：");
                String newName = INPUT.next();
                String newSex = null;
                while (true) {
                    System.out.println("请输入性别(1：男，0：女)：");
                    int newSexNum = INPUT.nextInt();
                    if (newSexNum == 1) {
                        newSex = "男";
                        break;
                    } else if (newSexNum == 0) {
                        newSex = "女";
                        break;
                    } else {
                        System.out.println("输入有误，请重新输入！");
                        continue;
                    }
                }
                System.out.println("请输入新的地址：");
                String newAddress = INPUT.next();
                User user = new User(newName,id,newSex,newAddress);
                boolean b = daoImpl.updateUserInfo(list, index, user);
                if (b){
                    System.out.println("修改身份信息成功！");
                    return;
                }
                else {
                    System.out.println("修改身份信息失败！");
                }
            }else {
                System.out.println("您输入的身份证号不存在");
                continue;
            }
        }
    }

    /**
     * 删除信息
     * @param list
     */
    @Override
    public void deleteUserInfo(LinkedList<User> list) {
        System.out.println("开始修改信息：");
        System.out.println("请输入修改的公民身份证号：");
        String deleteId = INPUT.next();
        boolean flag=false;
        User deleteUser=null;
        for (User user : list) {
            if (user.getId().equals(deleteId)){
                flag=true;
                deleteUser=user;
                break;
            }
        }
        if (flag){
            System.out.println("开始删除");
            boolean b = daoImpl.deleteUserInfo(list, deleteUser);
            if (b){
                System.out.println("删除成功");
            }
            else {
                System.out.println("删除失败");
            }
        }
        else {
            System.out.println("您输入的身份证不存在！");
        }
    }
}
